﻿using ons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CashAgent
{
    public class MyMsgListener : MessageListener
    {
        public MyMsgListener()
        {
        }

        ~MyMsgListener()
        {
        }

        public override ons.Action consume(Message value, ConsumeContext context)
        {
            Byte[] text = Encoding.Default.GetBytes(value.getBody());
            Console.WriteLine(Encoding.UTF8.GetString(text));
            return ons.Action.CommitMessage;
        }
    }


    public class MyMsgOrderListener : MessageOrderListener
    {
        public MyMsgOrderListener()
        {

        }

        ~MyMsgOrderListener()
        {
        }

        public override ons.OrderAction consume(Message value, ConsumeOrderContext context)
        {
            Byte[] text = Encoding.Default.GetBytes(value.getBody());
            Console.WriteLine(Encoding.UTF8.GetString(text));
            return ons.OrderAction.Success;
        }
    }


    public class OnscSharp
    {
        private static Producer _producer;
        private static PushConsumer _consumer;
        private static OrderConsumer _orderconsumer;
        private static OrderProducer _orderproducer;
        private static MyMsgListener _listen;
        private static MyMsgOrderListener _order_listen;
        private static object s_SyncLock = new Object();

        //public static String MQTT_BROKER = "tcp://://://mqf-a4qv2bek8n.mqtt.aliyuncs.com:1883";
        //public static String MQTT_ACCESS = "LTAINbRHnHe76MFe";
        //public static String MQTT_SECRET = "mdeasjuO0TtDnzKY53DgWBKanXRvGa";
        //public static String MQTT_TOPIC = "cashier";
        //public static String MQTT_GID = "GID_BUS";
        private static string Ons_Topic = "cashier";
        private static string Ons_ProducerID = "xxxxxxxxxxx";
        private static string Ons_AccessKey = "LTAINbRHnHe76MFe";
        private static string Ons_SecretKey = "mdeasjuO0TtDnzKY53DgWBKanXRvGa";
        private static string Ons_ConsumerId = "xxxxxxxxxxxx";

        public static void SendMessage(string msgBody, String tag = "RegisterLog")
        {
            Message msg = new Message(Ons_Topic, tag, msgBody);
            msg.setKey(Guid.NewGuid().ToString());
            try
            {
                SendResultONS sendResult = _producer.send(msg);
                Console.WriteLine("send success {0}", sendResult.getMessageId());
            }
            catch (Exception ex)
            {
                Console.WriteLine("send failure{0}", ex.ToString());
            }
        }

        public static void SendOrderMessage(string msgBody, String tag = "RegisterLog", String key = "test")
        {
            Message msg = new Message(Ons_Topic, tag, msgBody);
            msg.setKey(Guid.NewGuid().ToString());
            try
            {
                SendResultONS sendResult = _orderproducer.send(msg, key);
                Console.WriteLine("send success {0}", sendResult.getMessageId());
            }
            catch (Exception ex)
            {
                Console.WriteLine("send failure{0}", ex.ToString());
            }
        }

        public static void StartPushConsumer()
        {
            _listen = new MyMsgListener();
            _consumer.subscribe(Ons_Topic, "*", _listen);
            _consumer.start();
        }

        public static void StartOrderConsumer()
        {
            _order_listen = new MyMsgOrderListener();
            _orderconsumer.subscribe(Ons_Topic, "*", _order_listen);
            _orderconsumer.start();
        }

        public static void shutdownPushConsumer()
        {
            _consumer.shutdown();
        }

        public static void shutdownOrderConsumer()
        {
            _orderconsumer.shutdown();
        }

        public static void StartProducer()
        {
            _producer.start();
        }

        public static void ShutdownProducer()
        {
            _producer.shutdown();
        }


        public static void StartOrderProducer()
        {
            _orderproducer.start();
        }

        public static void ShutdownOrderProducer()
        {
            _orderproducer.shutdown();
        }

        private static ONSFactoryProperty getFactoryProperty()
        {
            ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
            factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_ConsumerId);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_ProducerID);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, Ons_Topic);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "E://log");
            return factoryInfo;
        }

        public static void CreatePushConsumer()
        {

            _consumer = ONSFactory.getInstance().createPushConsumer(getFactoryProperty());
        }

        public static void CreateProducer()
        {

            _producer = ONSFactory.getInstance().createProducer(getFactoryProperty());
        }


        public static void CreateOrderConsumer()
        {

            _orderconsumer = ONSFactory.getInstance().createOrderConsumer(getFactoryProperty());
        }

        public static void CreateOrderProducer()
        {

            _orderproducer = ONSFactory.getInstance().createOrderProducer(getFactoryProperty());
        }
    }

    class ConsumerAndProducerForEx
    {
        //static void Main(string[] args)
        //{
        //    OnscSharp.CreateProducer();
        //    OnscSharp.CreatePushConsumer();
        //    OnscSharp.StartPushConsumer();
        //    OnscSharp.StartProducer();
        //    System.DateTime beforDt = System.DateTime.Now;
        //    for (int i = 0; i < 1000; ++i)
        //    {
        //        OnscSharp.SendMessage("dewdewdew");
        //    }
        //    System.DateTime endDt = System.DateTime.Now;
        //    System.TimeSpan ts = endDt.Subtract(beforDt);
        //    Console.WriteLine("per message:{0}ms.", ts.TotalMilliseconds / 10000);
        //    OnscSharp.ShutdownProducer();
        //    OnscSharp.shutdownPushConsumer();
        //}
    }
}
